using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// This rule validates that there are not two Classes with the Same ClassName.
    /// </summary>
    public class DMAssemblyDuplicateClassNameRule : IBusinessObjectRule
    {
        private DMAssembly _dmAssembly;
        private string _className;

        public DMAssemblyDuplicateClassNameRule()
        {
//            if (dmAssembly == null) throw new ArgumentNullException("dmAssembly");
//            _dmAssembly = dmAssembly;
        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmAssembly = (DMAssembly) bo;
            if (_dmAssembly.Classes.Count < 2) return true;
            BusinessObjectCollection<DMClass> classes = _dmAssembly.Classes;
            classes.Sort("ClassNameBO", true, true);
            string previousClassName = "";
            foreach (DMClass dmClass in classes)
            {
                if (String.IsNullOrEmpty(dmClass.ClassNameBO)) continue;
                if (dmClass.ClassNameBO.Equals(previousClassName))
                {
                    _className = previousClassName;
                    return false;
                }
                previousClassName = dmClass.ClassNameBO;
            }
            return true;
        }

        public string Name
        {
            get { return "DMAssemblyDuplicateClassName"; }
        }

        public string Message
        {
            get
            {
                return "The Assembly '" + _dmAssembly +
                       "' cannot have more than one Class with the same name('" + _className + "').";
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Error; }
        }
    }
}